package com.xyht.sca_s.student_manage_system.modules.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsDormitoryType;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsWnsChargeClassificationRelation;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryTypeAddReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryTypeDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsDormitoryTypeUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.resp.SmsDormitoryTypeResp;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsDormitoryTypeMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsWnsChargeClassificationRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.service.SmsDormitoryTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.dormitory.constant.chargeTypeConstant.CHARGE_TYPE_DORMITORY;

/**
 * <p>
 * 宿舍类型管理表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-15
 */
@Service
public class SmsDormitoryTypeServiceImpl extends ServiceImpl<SmsDormitoryTypeMapper, SmsDormitoryType> implements SmsDormitoryTypeService {


    @Resource
    private SmsDormitoryTypeMapper dormitoryTypeMapper;
    @Resource
    private SmsWnsChargeClassificationRelationMapper smsWnsChargeClassificationRelationMapper;

    @Override
    public ResponseResult addDormitoryType(SmsDormitoryTypeAddReq addReq, String userId) {
        if (isNullOrEmpty(addReq.getTypeName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验名字是否重复
        List<SmsDormitoryType> dormitoryTypeList = dormitoryTypeMapper.selectList(new LambdaQueryWrapper<>());
        if (!dormitoryTypeList.isEmpty()) {
            List<SmsDormitoryType> typeNameList = dormitoryTypeList.stream()
                    .filter(i -> i.getTypeName().equals(addReq.getTypeName())).collect(Collectors.toList());
            if (!typeNameList.isEmpty()) {
                return CommonResult.failed(CommonCodeEnum.DORMITORY_TYPE_NAME_EXIST);
            }
        }
        SmsDormitoryType smsDormitoryType = new SmsDormitoryType();
        smsDormitoryType.setTypeName(addReq.getTypeName());
        smsDormitoryType.setDescription(addReq.getDescription());
        smsDormitoryType.setUserId(userId);
        smsDormitoryType.setBedNumber(addReq.getBedNumber());
        dormitoryTypeMapper.insert(smsDormitoryType);

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deletedDormitoryType(SmsDormitoryTypeDeletedReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsDormitoryType smsDormitoryType = dormitoryTypeMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(smsDormitoryType)) {
            return CommonResult.failed(CommonCodeEnum.DORMITORY_TYPE_NOT_EXIST);
        }
        //删除分类之前查看分类是否绑定收费类别
        Integer count = smsWnsChargeClassificationRelationMapper.selectCount(new LambdaQueryWrapper<SmsWnsChargeClassificationRelation>()
                .eq(SmsWnsChargeClassificationRelation::getClassificationId, deletedReq.getId())
                .eq(SmsWnsChargeClassificationRelation::getType, CHARGE_TYPE_DORMITORY));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.CHARGE_BIND_EXIST);
        }
        dormitoryTypeMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateDormitoryType(SmsDormitoryTypeUpdateReq updateReq) {
        if (isNullOrEmpty(updateReq.getTypeName(),updateReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsDormitoryType smsDormitoryType = dormitoryTypeMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsDormitoryType)) {
            return CommonResult.failed(CommonCodeEnum.DORMITORY_TYPE_NOT_EXIST);
        }
        //校验名字是否重复
        if (!smsDormitoryType.getTypeName().equals(updateReq.getTypeName())) {
            List<SmsDormitoryType> dormitoryTypeList = dormitoryTypeMapper.selectList(new LambdaQueryWrapper<>());
            if (!dormitoryTypeList.isEmpty()) {
                List<SmsDormitoryType> typeNameList = dormitoryTypeList.stream()
                        .filter(i -> i.getTypeName().equals(updateReq.getTypeName())).collect(Collectors.toList());
                if (!typeNameList.isEmpty()) {
                    return CommonResult.failed(CommonCodeEnum.DORMITORY_TYPE_NAME_EXIST);
                }
            }
        }

        BeanUtils.copyProperties(updateReq,smsDormitoryType);
        dormitoryTypeMapper.updateById(smsDormitoryType);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getDormitoryTypeList(Integer pageNum, Integer pageSize, String name) {
        Page<SmsDormitoryType> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsDormitoryType> lambdaQueryWrapper = new LambdaQueryWrapper<SmsDormitoryType>().orderByAsc(SmsDormitoryType::getTypeName);
        if (!isNullOrEmpty(name)) {
            lambdaQueryWrapper.like(SmsDormitoryType::getTypeName,name);
        }
        dormitoryTypeMapper.selectPage(page,lambdaQueryWrapper);
        List<SmsDormitoryTypeResp> dormitoryTypeList = page.getRecords()
                .stream()
                .map(smsDormitoryType -> {
                    SmsDormitoryTypeResp smsDormitoryTypeResp = new SmsDormitoryTypeResp();
                    BeanUtils.copyProperties(smsDormitoryType,smsDormitoryTypeResp);
                    return smsDormitoryTypeResp;
                })
                .collect(Collectors.toList());
        int total = (int)page.getTotal();
        return CommonResult.success(dormitoryTypeList,total);
    }
}
